package com.tuishan.myapplication.Adapters;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.google.gson.Gson;
import com.tuishan.myapplication.Model.UserLogModel;
import com.tuishan.myapplication.R;

import java.text.SimpleDateFormat;

/**
 * <pre>
 *     time   : 2021/04/17
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class UserLogAdapter extends PagedListAdapter<UserLogModel, UserLogAdapter.RecyclerViewHolder> {

    public UserLogAdapter() {
        super(DIFF_CALLBACK);
    }

    @NonNull
    @Override
    public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items_user_log, parent, false);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
        UserLogModel concert = getItem(position);
        if (concert != null) {
            String typeStr=""; //1登录 2交易 3安全
            switch (concert.getSource()){
                case 1:
                    typeStr="登录";
                    break;
                case 2:
                    typeStr="交易";
                    break;
                case 3:
                    typeStr="安全";
                    break;
            }
           String ResultStr=""; //1成功2失败
            switch (concert.getResult()){
                case 1:
                    ResultStr="成功";
                    break;
                case 2:
                    ResultStr="失败";
                    break;
            }
            holder.user_log_logType.setText(typeStr);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String startTime = formatter.format(TimeUtils.millis2Date((long)(concert.getCreateTime())*1000));
            String endTime = formatter.format(TimeUtils.millis2Date((long)(concert.getUpdateTime())*1000));
            holder.user_log_startTime.setText(startTime);
            holder.user_log_endTime.setText(endTime);
            holder.user_log_pageNum.setText(concert.getPageNum()+"");
            holder.user_log_result.setText(ResultStr);
        }
    }

    public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
        TextView user_log_logType;
        TextView user_log_startTime;
        TextView user_log_endTime;
        TextView user_log_pageNum;
        TextView user_log_result;

        public RecyclerViewHolder(@NonNull View itemView) {
            super(itemView);
            user_log_logType = itemView.findViewById(R.id.user_log_logType);
            user_log_startTime = itemView.findViewById(R.id.user_log_startTime);
            user_log_endTime = itemView.findViewById(R.id.user_log_endTime);
            user_log_pageNum = itemView.findViewById(R.id.user_log_pageNum);
            user_log_result = itemView.findViewById(R.id.user_log_result);
        }
    }

    private static DiffUtil.ItemCallback<UserLogModel> DIFF_CALLBACK =
            new DiffUtil.ItemCallback<UserLogModel>() {
                @Override
                public boolean areItemsTheSame(UserLogModel oldConcert, UserLogModel newConcert) {
                    return oldConcert.getId()==newConcert.getId();
                }
                @Override
                public boolean areContentsTheSame(UserLogModel oldConcert,
                                                  UserLogModel newConcert) {
                    return oldConcert.equals(newConcert);
                }
            };
}
